home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Timeline / Play Timeline.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  5.2 KB  |  147 lines

  1. //*************** GLOBALS VARS *****************
  2.  
  3. var helpDoc = MM.HELP_behPlayTimeline;
  4.  
  5. //******************* BEHAVIOR FUNCTION **********************
  6.  
  7. //Starts a timeline playing from the current frame.
  8. //Accepts the following arguments:
  9. //  tmLnName - the name of the timeline to play (ex: Timeline1)
  10. //
  11. //Designed to work in conjunction with Dreamweaver's Timeline Inspector.
  12. //The Timeline Inspector creates a JS function called MM_initTimelines(),
  13. //which puts all the timeline settings in a multidimensional array, saved
  14. //into a document property called document.MM_Time.
  15. //
  16. //My function initializes the timeline by calling MM_initTimelines.
  17. //Next it starts a timer using setTimeout(), which recursively calls this
  18. //again after the elapsed time. I use myID to identify recursive calls.
  19. //Then it checks the data arrays and sets all sprites to their new positions
  20. //and sets other properties. For behavior sprites it evals the behavior call.
  21.  
  22. function MM_timelinePlay(tmLnName, myID) { //v1.2
  23.   //Copyright 1997 Macromedia, Inc. All rights reserved.
  24.   var i,j,tmLn,props,keyFrm,sprite,numKeyFr,firstKeyFr,propNum,theObj,firstTime=false;
  25.   if (document.MM_Time == null) MM_initTimelines(); //if *very* 1st time
  26.   tmLn = document.MM_Time[tmLnName];
  27.   if (myID == null) { myID = ++tmLn.ID; firstTime=true;}//if new call, incr ID
  28.   if (myID == tmLn.ID) { //if Im newest
  29.     setTimeout('MM_timelinePlay("'+tmLnName+'",'+myID+')',tmLn.delay);
  30.     fNew = ++tmLn.curFrame;
  31.     for (i=0; i<tmLn.length; i++) {
  32.       sprite = tmLn[i];
  33.       if (sprite.charAt(0) == 's') {
  34.         if (sprite.obj) {
  35.           numKeyFr = sprite.keyFrames.length; firstKeyFr = sprite.keyFrames[0];
  36.           if (fNew >= firstKeyFr && fNew <= sprite.keyFrames[numKeyFr-1]) {//in range
  37.             keyFrm=1;
  38.             for (j=0; j<sprite.values.length; j++) {
  39.               props = sprite.values[j]; 
  40.               if (numKeyFr != props.length) {
  41.                 if (props.prop2 == null) sprite.obj[props.prop] = props[fNew-firstKeyFr];
  42.                 else        sprite.obj[props.prop2][props.prop] = props[fNew-firstKeyFr];
  43.               } else {
  44.                 while (keyFrm<numKeyFr && fNew>=sprite.keyFrames[keyFrm]) keyFrm++;
  45.                 if (firstTime || fNew==sprite.keyFrames[keyFrm-1]) {
  46.                   if (props.prop2 == null) sprite.obj[props.prop] = props[keyFrm-1];
  47.                   else        sprite.obj[props.prop2][props.prop] = props[keyFrm-1];
  48.         } } } } }
  49.       } else if (sprite.charAt(0)=='b' && fNew == sprite.frame) eval(sprite.value);
  50.       if (fNew > tmLn.lastFrame) tmLn.ID = 0;
  51.   } }
  52. }
  53.  
  54. //******************* API **********************
  55.  
  56. //Checks for the existence of timelines.
  57. //If none exist, returns false so this Action is grayed out.
  58.  
  59. function canAcceptBehavior(){
  60.   var allScripts,i,theScript,timelineExists;
  61.  
  62.   timelineExists = false;
  63.   allScripts = getObjectTags("document","script");
  64.   for (i in allScripts) {
  65.     theScript = ""+allScripts[i];
  66.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  67.       timelineExists = true;
  68.       break;
  69.   } }
  70.   return (timelineExists);
  71. }
  72.  
  73.  
  74.  
  75. //Returns a Javascript function to be inserted in HTML head with script tags.
  76.  
  77. function behaviorFunction(){
  78.   return "MM_timelinePlay";
  79. }
  80.  
  81.  
  82.  
  83. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  84. //Returns one arg: the selected timeline name.
  85.  
  86. function applyBehavior() {
  87.   menuIndex = document.theForm.menu.selectedIndex;  //get menu selection index
  88.   timelineName = document.theForm.menu.options[menuIndex].text; //gets selected string
  89.   return "MM_timelinePlay('"+timelineName+"')";
  90. }
  91.  
  92.  
  93.  
  94. //Passed the function call above, extracts the args and reloads the UI.
  95. //With arg timelineName, scans the menu for a matching item, and selects it.
  96. //If the name is not found, it gives an error msg.
  97.  
  98. function inspectBehavior(upStr){
  99.   var timelineName,menuLength,i;
  100.   var argArray = new Array;
  101.   var found = false;
  102.  
  103.   argArray = extractArgs(upStr); //get args
  104.   if (argArray.length == 2) {  //should be exactly 2 arg (first arg is fn name)
  105.     timelineName = argArray[1];
  106.     menuLength = document.theForm.menu.options.length;
  107.     for (var i=0; i<menuLength; i++) {  //search menu for matching timeline name
  108.       if (document.theForm.menu.options[i].text == timelineName) { //if found
  109.         document.theForm.menu.selectedIndex = i;  //make it selected
  110.         found = true;
  111.         break;
  112.       }
  113.     }
  114.     if (!found) alert(errMsg(MSG_TimelineNotFound,timelineName));
  115.   }
  116. }
  117.  
  118.  
  119.  
  120. //***************** LOCAL FUNCTIONS  ******************
  121.  
  122.  
  123. //Load the select menu with timeline names.
  124.  
  125. function initializeUI(){
  126.   var i,j,startPos,endPos,theName,theScript;
  127.   var menuIndex = 0;
  128.   var allScripts = new Array;
  129.  
  130.   allScripts = getObjectTags("document","script");
  131.   for (i in allScripts) {
  132.     theScript = ""+allScripts[i];
  133.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  134.       j = theScript.indexOf('].MM_Name');
  135.       while (j != -1) {
  136.         startPos = theScript.indexOf('"',++j);
  137.         endPos = theScript.indexOf('"',++startPos);
  138.         if (0 < startPos && startPos < endPos) {
  139.           theName = theScript.substring(startPos,endPos);
  140.           document.theForm.menu.options[menuIndex++] = new Option(theName);
  141.         }
  142.         j = theScript.indexOf('].MM_Name',j+1);
  143.       }
  144.     }
  145.   }
  146. }
  147.